//
//  HLSubTagViewController.m
//  HL-百思不得姐
//
//  Created by admin on 16/5/12.
//  Copyright © 2016年 CHL. All rights reserved.
//

#import "HLSubTagViewController.h"
#import <AFNetworking/AFNetworking.h>
#import <MJExtension/MJExtension.h>
#import "HLSubTagItem.h"
#import "HLSubTagTableViewCell.h"

@interface HLSubTagViewController ()

@property (strong,nonatomic) NSArray *subTagArray;

@end

@implementation HLSubTagViewController

static NSString *ID = @"cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //加载数据
    [self loadData];
    
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"HLSubTagTableViewCell" bundle:nil] forCellReuseIdentifier:ID];
    
    self.title = @"推荐标签";
    
     // 处理cell分割线:万能方式(重写cell的setFrame) 1.取消系统自带分割线 2.把tableView背景色设置为分割线的背景色 3.重写setFrame
    //取消系统自带分割线
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.backgroundColor = HLColor(220, 220, 221);
}

#pragma mark - 加载标签数据
- (void)loadData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    parameter[@"a"] = @"tag_recommend";
    parameter[@"c"] = @"topic";
    parameter[@"action"] = @"sub";
    
    [manager GET:@"http://api.budejie.com/api/api_open.php" parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [responseObject writeToFile:@"/Applications/程序/大神班/项目/HL-百思不得姐/HL-百思不得姐/Classes/New(新帖)/new.plist" atomically:YES];
        //转换成模型数组
        self.subTagArray = [HLSubTagItem mj_objectArrayWithKeyValuesArray:responseObject];
        //刷新列表
        [self.tableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
}

#pragma mark - UITableViewDataResource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.subTagArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HLSubTagTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
//    if (cell == nil) {
//        cell = [[HLSubTagTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
//    }
    HLSubTagItem *subTagItem = self.subTagArray[indexPath.row];
    cell.item = subTagItem;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

@end
